package com.slz3.business.module.configManagement.conZoneType.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.configManagement.conZoneTemplate.domain.ConZoneTemplate;
import com.slz3.business.module.configManagement.conZoneType.domain.ConZoneType;
import com.slz3.business.module.configManagement.conZoneType.domain.ConZoneTypeFactory;
import com.slz3.business.module.configManagement.conZoneType.steward.ConZoneTypeSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ConZoneTypeServiceImpl implements ConZoneTypeService {
    @Override
    public ConZoneType save(ConZoneType conZoneType) {
        return Repositorys.conZoneType.save(ConZoneTypeFactory.initBaseFields(conZoneType));
    }
    @Override
    public Page<ConZoneType> page(MultiValueMap<String, String> params) {
        Page<ConZoneType> conZoneTypePage = Repositorys.conZoneType.findAll(
                ConZoneTypeSpecification.findJoin(params), PageableSteward.extract(params)
        );
        // join
        Map<UUID, ConZoneTemplate> zoneTemplateMap = Repositorys.conZoneTemplate.findAllById(
                conZoneTypePage.stream().map(ConZoneType::getFkZoneTemplate).collect(Collectors.toList())
        ).stream().collect(Collectors.toMap(ConZoneTemplate::getUuid, Function.identity()));
        //
        List<UUID> fkUpperZoneList = conZoneTypePage.stream().filter(item -> item.getFkUpperZone() != null)
                .map(ConZoneType::getFkUpperZone).collect(Collectors.toList());
        List<ConZoneType> upperZoneTypeList = Repositorys.conZoneType.findAllById(fkUpperZoneList);
        Map<UUID, ConZoneType> upperZoneTypeMap = upperZoneTypeList.stream().collect(Collectors.toMap(ConZoneType::getUuid, Function.identity()));
        //
        conZoneTypePage.forEach(item -> {
            item.setZoneTemplate(zoneTemplateMap.get(item.getFkZoneTemplate()));
            item.setUpperZoneType(upperZoneTypeMap.get(item.getFkUpperZone()));
        });
        return conZoneTypePage;
    }


    @Override
    public ConZoneType modify(ConZoneType conZoneTypeNew) {
        return Repositorys.conZoneType.saveAndFlush(ConZoneTypeFactory.initBaseFields(conZoneTypeNew));
    }

    @Override
    public Optional<ConZoneType> single(UUID uuid) {
        return Repositorys.conZoneType.findById(uuid);
    }

    @Override
    public List<ConZoneType> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.conZoneType.findAllById(uuids);
    }

    @Override
    public List<ConZoneType> delete(List<ConZoneType> conZoneTypeList) {
        conZoneTypeList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            ConZoneTypeFactory.initBaseFields(item);
        });
        return Services.conZoneTypeTransactional.batchModify(conZoneTypeList);
    }

}
